package com.yeyuanqi.hottop.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yeyuanqi.hottop.annotation.AccessLimit;
import com.yeyuanqi.hottop.common.Result;
import com.yeyuanqi.hottop.entity.Comment;
import com.yeyuanqi.hottop.entity.TJinrihot;
import com.yeyuanqi.hottop.service.CommentService;
import com.yeyuanqi.hottop.vo.PageComment;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 评论前端控制器
 *
 * @author yeyuanqi
 * @date  2022/02/26
 */
@RestController
public class CommentController {
    @Autowired
    CommentService commentService;

    Logger logger = LoggerFactory.getLogger(CommentController.class);

    /**
     * 分页查询所有评论
     */
    @RequiresAuthentication
    @GetMapping("/comment")
    public Result getCommentByPage(@RequestParam(defaultValue = "1") Integer currentPage, @RequestParam(defaultValue = "5") Integer pageSize ) {
        Page page = new Page(currentPage, pageSize);
        //实体模型集合对象转换为VO对象集合
        List<PageComment> comments = commentService.getParentCommentListByTimeDesc();
        for (PageComment comment : comments) {

            List<PageComment> reply = commentService.getCommentByParentId(comment.getId());
            comment.setReplyComments(reply);
        }
        Page<PageComment> pages = new Page<PageComment>();
        page.setRecords(comments);
        page.setTotal(comments.size());
        //相除向上取整
        page.setPages((int)Math.ceil((double)comments.size()/pageSize));
        //Assert.notNull(blog, "该博客已删除！");
        return Result.succ(page);

    }


    /**
     * 提交评论
     */
    @PostMapping("/comment/add")
    public Result edit(@Validated @RequestBody Comment comment, HttpServletRequest request) {

        if (comment.getContent().contains("<script>") || comment.getNickname().contains("<script>") ) {
            return Result.fail("非法输入");
        }
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
//        SimpleDateFormat dateFormat_min=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");//设置当前时间的格式，为年-月-日 时-分-秒
        try {
            comment.setCreateTime( sdf.parse(dateNowStr));
            commentService.save(comment);
            return Result.succ("提交成功");
        }catch (Exception e){
            logger.info(e.getMessage());
            return Result.fail(e.getMessage());
        }}

}
